import Mock from 'mockjs'
import { baseUrl } from '@/utils/global'
import * as login from './modules/login'
import * as user from './modules/user'
import * as menu from './modules/menu'
import * as goods from './modules/goods'
import * as brand from './modules/brand'
import * as category from './modules/goodsCategory'
import * as skuStock from './modules/skuStock'
import * as goodsAttribute from './modules/goodsAttribute'
import * as upload from './modules/upload'
import * as regimental from './modules/regimental'
import * as userAddress from './modules/userAddress'
import * as order from './modules/order'
import * as afterSale from './modules/afterSale'
import * as supplier from './modules/supplier'
import * as freight from './modules/freight'
import * as city from './modules/city'


// 1. 开启/关闭[所有模块]拦截, 通过调[openMock参数]设置.
// 2. 开启/关闭[业务模块]拦截, 通过调用fnCreate方法[isOpen参数]设置.
// 3. 开启/关闭[业务模块中某个请求]拦截, 通过函数返回对象中的[isOpen属性]设置.
// let openMock = true
let openMock = true
fnCreate(login, openMock)
fnCreate(user, openMock)
fnCreate(menu, openMock)
fnCreate(goods, openMock)
fnCreate(brand, openMock)
fnCreate(category, openMock)
fnCreate(skuStock, openMock)
fnCreate(goodsAttribute, openMock)
fnCreate(upload, openMock)
fnCreate(regimental, openMock)
fnCreate(userAddress, openMock)
fnCreate(order, openMock)
fnCreate(afterSale, openMock)
fnCreate(supplier, openMock)
fnCreate(freight, openMock)
fnCreate(city, openMock)



/**
 * 创建mock模拟数据
 * @param {*} mod 模块
 * @param {*} isOpen 是否开启?
 */
function fnCreate (mod, isOpen = true) {

  if (isOpen) {
    for (var key in mod) {
      ((res) => {
        if (res.isOpen !== false) {
          let url = baseUrl
          if(!url.endsWith("/")) {
            url = url + "/"
          }
          url = url + res.url
          Mock.mock(new RegExp(url), res.type, (opts) => {
            opts['data'] = opts.body ? JSON.parse(opts.body) : null
            delete opts.body
            console.log('\n')
            console.log('%cmock拦截, 请求: ', 'color:blue', opts)
            console.log('%cmock拦截, 响应: ', 'color:blue', res.data)
            return res.data
          })
        }
      })(mod[key]() || {})
    }
  }
}
